﻿Type=Activity
Version=3.15
@EndOfDesignText@
#Region Module Attributes
	#FullScreen: False
	#IncludeTitle: True
#End Region

'Activity module
Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.

End Sub

Sub Globals
	'These global variables will be redeclared each time the activity is created.
	'These variables can only be accessed from this module.

	Dim scvList As HorizontalScrollView
	Dim scvHeader As HorizontalScrollView
	Dim btnRefresh As Button
	Dim btnPre As Button
	Dim btnNext As Button
	Dim edtCXTS As EditText
	Dim edtBZ As EditText
	
  Dim pnlHeader As Panel
  Dim pnlTable As Panel
  Dim NumberOfColumns,RowHeight, RowHeight_1  As Int
  Dim HeaderColor, LineColor, CellColor, FontColor, HeaderFontColor As Int
  Dim ColLineWidth, RowLineWidth As Int
	Dim SumRecordCount,CurPage,SumPages As Int
  Dim FontSize As Float
  
  'Dim Alignment As Int
  Dim SelectedRow As Int
  Dim SelectedRowColor As Int : SelectedRowColor=Colors.LightGray
	
	Dim FInitHeader As Boolean 
	Dim FGetSQL,FTitle As String
	
  'Table settings
  HeaderColor = Colors.Gray
  NumberOfColumns = 2 'will be overwritten when loading from CSV file.
  ColLineWidth = 1dip
  RowLineWidth = 1dip
  RowHeight_1 = 30dip
  RowHeight=RowHeight_1+RowLineWidth
  LineColor = Colors.Black
  CellColor = Colors.White
  FontColor = Colors.Black
  HeaderFontColor = Colors.White
  FontSize = 16
	
	FGetSQL = "SELECT JL.*,XM.MC AS XMMC   "
	FGetSQL = FGetSQL & "FROM JZ_ZCJL0 JL " 
	FGetSQL = FGetSQL & "LEFT JOIN ZD_ZCXM XM ON JL.XMBM=XM.BM " 

	FTitle = "查询支出记录"

End Sub

Sub Activity_Create(FirstTime As Boolean)
	
	Activity.LoadLayout("FormJZZCJLView")
	Activity.Title = FTitle '查询支出记录
	
	FInitHeader = False
	pnlHeader = scvHeader.Panel
	
	pnlTable = scvList.Panel
  pnlTable.Color = LineColor
  
  SelectedRow = -1
	
	InitLayOut
	InitData

End Sub

Sub Activity_Resume
	btnRefresh_Click
	
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub InitLayOut
	
	'高级菜单
	Activity.AddMenuItem("修改","MenuItemAdv")
	Activity.AddMenuItem("删除","MenuItemAdv")
End Sub

Sub MenuItemAdv_Click
	If Sender = "修改" Then
		EditZCJL
	Else If Sender = "删除" Then
		DeLeteZCJL
	End If
	
End Sub

Sub InitData
	edtCXTS.Text = "15" '15天范围

End Sub

Sub GetSumRecord
	Dim sSQL As String
	
	sSQL = "SELECT COUNT(*) FROM ("&FGetSQL&")"
	Log("SQL: " & sSQL)
	SumRecordCount = PublicVar.GV_SQLTool.ExecQuerySingleResult(sSQL)
End Sub

Sub SetTitleAndButton
	Activity.Title = FTitle & "[当前第"&CurPage&"页/共"&SumPages&"页]"
	
	If SumPages = 1 Then
		btnPre.Visible = False	
		btnNext.Visible = False	
	Else If CurPage=1 Then
		btnPre.Visible = False
		btnNext.Visible = True
	Else If CurPage = SumPages Then
		btnPre.Visible = True
		btnNext.Visible = False	
	End If
	
	ToastMessageShow("当前第"&CurPage&"页,共"&SumPages&"页", False)
	
End Sub

Sub LoadData(AStart As Int)
	Dim sSQL As String
	Dim DataSetList As Cursor
	Dim h(6) As String
	Dim w(6) As Int
	Dim iOffSet,iRecordOfScreen As Int
	Dim iID As Long
	
	iRecordOfScreen = GetRecordOfScreen
	iOffSet = (AStart -1) * iRecordOfScreen
	
	CurPage = AStart
	SumPages = SumRecordCount / iRecordOfScreen
	If SumPages =0 Then SumPages =1
	
	SetTitleAndButton
	
	
	h(0) = "支出日期"
	h(1) = "支出项目"
	h(2) = "金额"
	h(3) = "数量"
	h(4) = "单价"
	h(5) = "备注"
	
	w(0) = 130
	w(1) = 250
	w(2) = 80
	w(3) = 60
	w(4) = 60
	w(5) = 400
	
	ClearAll
	
	NumberOfColumns = h.Length
  SetHeader(h,w)
	
	sSQL = FGetSQL & "ORDER BY JL.ZCRQ DESC,JL.RowID DESC "
	sSQL = sSQL & "LIMIT "&iRecordOfScreen 
	If iOffSet>0 Then
		sSQL = sSQL & " OFFSET " & iOffSet 
	End If
	
	Log("SQL: " & sSQL)	
	DataSetList = PublicVar.GV_SQLTool.ExecQuery(sSQL)
	
	Dim data(NumberOfColumns) As String
	Dim Add(1) As String
	For i = 0 To DataSetList.RowCount - 1
		Dim Row() As String
		
		DataSetList.Position = i
		data(0) = DataSetList.GetString("ZCRQ") 
		data(1) = DataSetList.GetString("XMMC") 
		data(2) = DataSetList.GetString("JE")
		data(3) = DataSetList.GetString("SL")
		data(4) = DataSetList.GetString("DJ")
		data(5) = DataSetList.GetString("BZ")
		Row =data
		
		iID = DataSetList.GetLong("ID")
		Add(0) = iID
		
    AddRow(Row,w,Add)

	Next
	DataSetList.Close

End Sub

Sub ClearAll
    For i = pnlTable.NumberOfViews -1 To 0 Step -1
        pnlTable.RemoveViewAt(i)
    Next
    pnlTable.Height = 0
    SelectedRow = -1
End Sub

Sub SetHeader(Values() As String,Width() As Int)
    Dim iWidth As Int		
		iWidth = 1
		
		If FInitHeader Then Return
		
    For i = 0 To NumberOfColumns - 1
        Dim l As Label
  
				l.Initialize("header")
        l.Text = Values(i)			
				l.Gravity = Gravity.CENTER
        l.TextSize = FontSize
        l.Color = HeaderColor
        l.TextColor = HeaderFontColor
        l.Tag = i
        pnlHeader.AddView(l, iWidth, 0, Width(i) - ColLineWidth, RowHeight_1)
				iWidth = iWidth + Width(i)
    Next
		
		FInitHeader = True
End Sub

Sub AddRow(Values() As String,Width() As Int,Adds() As String)
    If Values.Length <> NumberOfColumns Then
        Log("Wrong number of values.")
        Return
    End If
    Dim lastRow As Int
    Dim iWidth As Int		
		iWidth = 1
		
    lastRow = NumberOfRows
    For i = 0 To NumberOfColumns - 1
        Dim l As Label
				
        l.Initialize("cell")
        l.Text = Values(i)
				
				Select i 
					Case 0,1,5
						l.Gravity = Gravity.LEFT + Gravity.CENTER_VERTICAL
					Case Else
						l.Gravity = Gravity.RIGHT + Gravity.CENTER_VERTICAL
				End Select
				
        l.TextSize = FontSize
        l.TextColor = FontColor
        l.Color=Colors.White
        Dim rc As RowCol
        rc.Initialize
        rc.Col = i
        rc.Row = lastRow
				rc.ID = Adds(0)
        l.Tag = rc
        pnlTable.AddView(l, iWidth, RowHeight * lastRow, Width(i) - ColLineWidth, RowHeight_1)
				iWidth = iWidth + Width(i)
    Next
    pnlTable.Height = NumberOfRows * RowHeight
End Sub

Sub Cell_Click
  Dim rc As RowCol
  Dim l As Label
  'Dim l0 As Label
  l = Sender
  rc = l.Tag
  SelectRow(rc.Row)
  'l0=GetView(rc.Row,0)
	
	PublicVar.GV_JZ_ZCJL_ID = rc.ID
     
End Sub

Sub Cell_LongClick
	Cell_Click
	
	EditZCJL
End Sub

Sub Header_Click
  Dim l As Label
  Dim col As Int
  l = Sender
  col = l.Tag
  'Activity.Title = "Header clicked: " & col
End Sub

Sub NumberOfRows As Int
    Return pnlTable.NumberOfViews / NumberOfColumns
End Sub

Sub GetRecordOfScreen As Int
	Return scvList.Height / RowHeight

End Sub

Sub SelectRow(Row As Int)
    'remove the color of previously selected row
    If SelectedRow > -1 Then
        For col = 0 To NumberOfColumns - 1
            GetView(SelectedRow, col).Color = CellColor
        Next
    End If
    SelectedRow = Row
    For col = 0 To NumberOfColumns - 1
        GetView(Row, col).Color = SelectedRowColor
    Next
End Sub

'Returns the label in the specific cell
Sub GetView(Row As Int, Col As Int) As Label
    Dim l As Label
    l = pnlTable.GetView(Row * NumberOfColumns + Col)
    Return l
End Sub

Sub scvHeader_ScrollChanged(Position As Int)
	'scvList.ScrollPosition = Position
End Sub
Sub scvList_ScrollChanged(Position As Int)
	scvHeader.ScrollPosition = Position
End Sub

Sub btnRefresh_Click
  Dim iCXTS As Int
	
	FGetSQL = "SELECT JL.*,XM.MC AS XMMC   "
	FGetSQL = FGetSQL & "FROM JZ_ZCJL0 JL " 
	FGetSQL = FGetSQL & "LEFT JOIN ZD_ZCXM XM ON JL.XMBM=XM.BM "
	FGetSQL = FGetSQL & "WHERE JL._FLAG<2 " '_FLAG 0 未同步 1已同步 2删除
	
	iCXTS = edtCXTS.Text
	If iCXTS >0 Then
		FGetSQL = FGetSQL & "AND JL.ZCRQ>DATE(DATE(),'-"& iCXTS &" DAY') "		
	End If
	
	If edtBZ.Text<>"" Then
		FGetSQL = FGetSQL & "AND JL.BZ LIKE '%"& edtBZ.Text &"%' "		
	End If
	
	GetSumRecord
	
	ToastMessageShow("共找到"&SumRecordCount&"笔记录", False)
	
	LoadData(1)	
End Sub
Sub btnPre_Click
	LoadData(CurPage -1)	
End Sub
Sub btnNext_Click
	LoadData(CurPage +1)		
End Sub

Sub EditZCJL
	If PublicVar.GV_JZ_ZCJL_ID = 0 Then
		ToastMessageShow("请先选择记录", True)
		Return
	End If
	
	StartActivity("JZ_ZCJL")

End Sub

Sub DeLeteZCJL
	Dim sSql As String
	Dim DataSetZCJL As Cursor
	Dim iZCID As Int
	

	If PublicVar.GV_JZ_ZCJL_ID = 0 Then
		ToastMessageShow("请先选择记录", True)
		Return
	End If
	
	sSql = "SELECT JL.*,XM.MC AS XMMC   "
	sSql = sSql & "FROM JZ_ZCJL0 JL " 
	sSql = sSql & "LEFT JOIN ZD_ZCXM XM ON JL.XMBM=XM.BM " 	
	sSql = sSql & "WHERE ID = " & PublicVar.GV_JZ_ZCJL_ID
	
	DataSetZCJL = PublicVar.GV_SQLTool.ExecQuery(sSql)		
	If DataSetZCJL.RowCount>0 Then
		DataSetZCJL.Position = 0
		
		iZCID = DataSetZCJL.GetInt("ZCID")
		
		If iZCID =0 Then
			sSql = "DELETE FROM JZ_ZCJL0 WHERE ID = " & PublicVar.GV_JZ_ZCJL_ID	
			
		Else
			sSql = "UPDATE JZ_ZCJL0 SET _FLAG = 2 WHERE ID = " & PublicVar.GV_JZ_ZCJL_ID	
		
		End If
		
		Log("SQL: " & sSql)
		Try
			PublicVar.GV_SQLTool.ExecNonQuery(sSql)
		Catch
			Log(LastException.Message)
			ToastMessageShow("删除失败"&CRLF&LastException.Message, True)	
			Return	
		End Try
	End If
	
	LoadData(CurPage)
End Sub